package org.gameg;

import org.apache.shiro.authc.*;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.apache.shiro.util.ByteSource;
import org.gameg.model.biz.SysUser;
import org.gameg.web.biz.LoginContoller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by Administrator on 2017/7/31.
 */
@Component
public class ShiroRealm extends AuthenticatingRealm {

    @Autowired
    private LoginContoller userManageController;

    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.out.println("数据验证中=================================");
        UsernamePasswordToken passwordToken = (UsernamePasswordToken) token;
        SysUser user = new SysUser();
        user.setUserName(passwordToken.getUsername());
        user = userManageController.getUser(user);
        SimpleAuthenticationInfo info = null;
        //盐值
        ByteSource credentialsalt = ByteSource.Util.bytes(user.getUserName());
        info = new SimpleAuthenticationInfo(user.getUserName(),user.getPassword(),credentialsalt,getName());
        return info;
    }

    public boolean login(SysUser sysUser){
        System.out.println("数据验证中=================================");
        SysUser user = new SysUser();
        user.setUserName(sysUser.getUserName());
        user = userManageController.getUser(user);
        boolean info = false;
        //盐值
        String algorithmName = "MD5";
        Object soure = sysUser.getPassword();
        Object salt = ByteSource.Util.bytes(sysUser.getUserName());
        Integer hashIterations = 1024;
        String pwd = (new SimpleHash(algorithmName,soure,salt,hashIterations)).toString();
        if (pwd.equals(user.getPassword())) {
            info = true;
        } else {
            info = false;
        }
        return info;
    }

    public static void main(String[] args) {
        String algorithmName = "MD5";
        Object soure = "123456";
        Object salt = ByteSource.Util.bytes("admin");
        Integer hashIterations = 1024;
        String a = (new SimpleHash(algorithmName,soure,salt,hashIterations)).toString();
        System.out.println(a);
    }

    /*@RequestMapping("register")
    @ResponseBody
    public Map<String,Object> register(User user){
        String algorithmName = "MD5";
        Object soure = user.getPwd();
        Object salt = ByteSource.Util.bytes(user.getName());
        Integer hashIterations = 1024;
        String a = (new SimpleHash(algorithmName,soure,salt,hashIterations)).toString();
        user.setPwd(a);
        User creat = SysUserOperate.getUser();
        if (creat!=null){
            user.setCreatePerson(creat.getId());
        }
        Map<String,Object> map = userService.register(user);
        SysLog sysLog = new SysLog();
        if (user.getId()!=null){
            sysLog.setMenuName("用户管理");
            sysLog.setMethodName(map.get("ff").toString());
            sysLog.setOprDetail("用户"+user.getName()+map.get("msg"));
            sysLogService.setLog(sysLog,user);
        }
        return map;
    }*/



}
